window.onload = function () {
    setTimeout(function () {
        var deptId = $('#deptId').val();
        if ($.isEmptyObject(deptId)) {
            getAllDept();
        } else {
            getAllDept(deptId);
        }
        var majorId = $('#majorId').val();
        if ($.isEmptyObject(majorId)) {
            getMajorByDeptId();
        } else {
            getMajorByDeptId(majorId);
        }
    }, 600);

}

var layer, table, form;
layui.use(['layer', 'laydate', 'table', 'form'], function () {
    layer = layui.layer;
    laydate = layui.laydate;
    table = layui.table;
    form = layui.form;

    // 监听院系下拉列表
    form.on("select(department)", function (data) {
        // 获取专业下拉列表
        getMajorByDeptId(data.value);
    });

    //自定义验证规则
    form.verify({
        clazzId: function (value) {
            if (value == '') {
                return '请填写班级编号';
            }
        },
        clazzName: function (value) {
            if (value == '') {
                return '请填写班级名称';
            }
        },
        department: function (value) {
            if (value == '') {
                return '请选择院系';
            }
        },
        major: function (value) {
            if (value == '') {
                return '请选择专业';
            }
        }
    });

    //监听提交
    form.on('submit(form)', function (data) {
        var param = data.field;
        param.deptId = param.department;
        param.majorId = param.major;

        var isModify = $('#isModify').val();
        if (isModify === 'true') {
            modifyDormitory(param);
        } else {
            addDormitory(param);
        }
        return false;
    });
});

function addDormitory(param) {
    $.ajax({
        type: "POST",
        url: "/common/addClazz",
        contentType: "application/json",
        sync: true,
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                // 弹出提示
                layer.msg('添加班级成功！', {anim: 3, icon: 6, time: 2000, shade: 0.1});
                setTimeout(function () {
                    // 关闭自身窗口
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 刷新父窗口
                    parent.layui.table.reload('clazzTable');//重新加载父页面中的数据表格
                }, 1000);
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}

function modifyDormitory(param) {
    $.ajax({
        type: "POST",
        url: "/common/modifyClazz",
        contentType: "application/json",
        sync: true,
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                // 弹出提示
                layer.msg('修改宿舍信息成功！', {anim: 3, icon: 6, time: 2000, shade: 0.1});
                setTimeout(function () {
                    // 关闭自身窗口
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 刷新父窗口
                    parent.layui.table.reload('clazzTable');//重新加载父页面中的数据表格
                }, 1000);
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}

/* 监测学号输入框，判断该是否已经录入 */
function checkDoemitoryIsExisted() {
    var isModify = $('#isModify').val();
    if (isModify === 'true') {
        return;
    }

    var param = {
        stuId: $('#stuId').val()
    };
    $.ajax({
        type: "POST",
        url: "/dormitory/checkDoemitoryIsExisted",
        //contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            // 该学生已录入
            if (result.data == true) {
                layer.msg('该学生已存在！', {anim: 6, icon: 5, time: 1500, shade: 0.1});
                $('#stuId').val('');
            }
        }
    });
}

/**
 * 获取所有院系
 * @param deptId
 */
function getAllDept(deptId) {
    $.ajax({
        type: "POST",
        url: "/common/getAllDept",
        contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            $("#department").empty();
            $("#department").append("<option value=''>请选择院系</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (deptId === o.deptId) {
                        opt = '<option value=' + o.deptId + ' selected>' + o.deptName + '</option>';
                    } else {
                        opt = '<option value=' + o.deptId + '>' + o.deptName + '</option>';
                    }
                    $("#department").append(opt);
                });
            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}

/**
 * 根据院系获取专业
 * @param majorId
 */
function getMajorByDeptId(majorId) {
    // 获取表单参数
    var deptId = $("#department option:selected").val();
    var param = {
        deptId: deptId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getMajorByDeptId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#major").empty();
            $("#major").append("<option value=''>请选择专业</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (majorId === o.majorId) {
                        opt = '<option value=' + o.majorId + ' selected>' + o.majorName + '</option>';
                    } else {
                        opt = '<option value=' + o.majorId + '>' + o.majorName + '</option>';
                    }
                    $("#major").append(opt);
                });
            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}